//
//  Live_Cell.m
//  pz_live
//
//  Created by caiyc on 2018/9/17.
//  Copyright © 2018年 caiyc. All rights reserved.
//   直播大厅的cell样式

#import "HostLive_Cell.h"
#import "OYCountDownManager.h"
#define kCountDownNotification @"cutdown"
@implementation HostLive_Cell
{
    NSInteger countDwonTime;
    NSInteger endtime;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    self.contents_View.layer.cornerRadius = 5;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(countDownNotification) name:kCountDownNotification object:nil];
    
    // Initialization code
}
- (IBAction)clickLive:(id)sender {
    self.live();
}
-(void)setViewAutoLayout{
    [self.image mas_makeConstraints:^(MASConstraintMaker *make) {
        
    }];
}
-(void)bindData:(NSDictionary *)dic{
    self.live_Data = dic;
    NSURL *brand_Url = [NSURL URLWithString:dic[@"logo"]];
   // [self.brand sd_setImageWithURL:brand_Url placeholderImage:[UIImage imageNamed:@"logo"] options:(SDWebImageOptions)];
    [self.brand sd_setImageWithURL:brand_Url placeholderImage:[UIImage imageNamed:@"logo"]];
    self.title.text = dic[@"prize_title"];
    NSURL *good_Url = [NSURL URLWithString:dic[@"thumb"]];
   [ self.image sd_setImageWithURL:good_Url placeholderImage:[UIImage imageNamed:@"logo"]];
    //self.price.text = [NSString stringWithFormat:@"%@元抢购",dic[@"price"]];
    self.price.textColor = PRICE_COLOR;
   // self.price.font = [UIFont fontWithName:@"Helvetica-Bold" size:20];
    
    NSString *price_text = [NSString stringWithFormat:@"抢购价：%@元",dic[@"price"]];
    NSDictionary *attrDict1 = @{NSFontAttributeName:[UIFont fontWithName: @"Helvetica-Bold" size: 13],
                                NSForegroundColorAttributeName:[UIColor blackColor] };
     NSAttributedString *attrStr1 = [[NSAttributedString alloc] initWithString: [price_text substringWithRange: NSMakeRange(0, price_text.length)] attributes: attrDict1];
    
    NSString *price_text1 =[NSString stringWithFormat: @"    原价：%@",dic[@"prize_price"]];
    NSDictionary *attrDict2 = @{NSFontAttributeName:[UIFont systemFontOfSize:12],NSForegroundColorAttributeName:RGBA(150, 150, 150, 1) };
    NSAttributedString *attrStr3 = [[NSAttributedString alloc] initWithString: price_text1 attributes: attrDict2];
    
//    NSString *old_price = [NSString stringWithFormat:@"  ¥%@",dic[@"price_bids"]];
//    NSDictionary *attribtDic = @{NSStrikethroughStyleAttributeName: [NSNumber numberWithInteger:NSUnderlineStyleSingle],NSForegroundColorAttributeName:[UIColor grayColor]};
//    NSMutableAttributedString *attribtStr = [[NSMutableAttributedString alloc]initWithString:[old_price substringWithRange: NSMakeRange(2, price_text.length-2)] attributes:attribtDic];

    NSMutableAttributedString *attributedStr03 = [[NSMutableAttributedString alloc] initWithAttributedString: attrStr1];
    [attributedStr03 appendAttributedString:attrStr3];
   // [attributedStr03 appendAttributedString:attribtStr];
    self.price.attributedText = attributedStr03;
    self.describe.text = dic[@"prize_desc"];
    countDwonTime = [self.live_Data[@"start_time"] integerValue] -  [self.live_Data[@"now_time"] integerValue];
    endtime =  [self.live_Data[@"end_time"] integerValue] -  [self.live_Data[@"now_time"] integerValue];
    
    if(endtime>0)
    {
        self.liveStatus.text = @"当前可以直播";
    }
    else{
        self.time_1.hidden = 1;
        self.time_2.hidden = 1;
        self.time_3.hidden = 1;
        self.time_4.hidden = 1;
        self.mao.hidden = 1;
        self.liveStatus.text = @"不在直播时间";
        self.start_Live.backgroundColor = viewcontrollerColor;
        self.start_Live.enabled = 0;
    }
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        // 监听通知
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(countDownNotification) name:kCountDownNotification object:nil];
    }
    return self;
}
// xib创建
- (instancetype)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self) {
        // 监听通知
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(countDownNotification) name:OYCountDownNotification object:nil];
    }
    return self;
}
- (void)countDownNotification {
    /// 计算倒计时
   // NSInteger countDown = [self.live_Data[@"start_time"] integerValue] -  [self.live_Data[@"now_time"] integerValue];
    if (countDwonTime <= 0) {
        // 倒计时结束时回调
       // xxxx(使用代理或block)
        
        return;
    }
    [self getMMSSFromSS:countDwonTime];
    countDwonTime--;
    /// 重新赋值
  //  self.timeLabel.text = [NSString stringWithFormat:@"倒计时%02zd:%02zd:%02zd", countDown/3600,       (countDown/60)%60, countDown%60];
}
-(NSString *)getMMSSFromSS:(NSInteger )totalTime{
    NSInteger seconds = totalTime;
    //format of minute
    NSString *str_minute = [NSString stringWithFormat:@"%ld",seconds/60];
    if(str_minute.length>1){
        self.time_1.text = [str_minute substringWithRange:NSMakeRange(0, 1)];
        self.time_2.text =  [str_minute substringWithRange:NSMakeRange(1, 1)];
    }else{
        self.time_2.text = str_minute;
    }
    //format of second
    NSString *str_second = [NSString stringWithFormat:@"%ld",seconds%60];
    if(str_second.length>1){
        self.time_3.text = [str_second substringWithRange:NSMakeRange(0, 1)];
        self.time_4.text =  [str_second substringWithRange:NSMakeRange(1, 1)];
    }else{
        self.time_3.text = @"0";
        self.time_4.text = str_second;
    }
    
//    if(self.isrobing==1){
//
//        self.time_lab1.backgroundColor = PRICE_COLOR;
//        self.time_lab2.backgroundColor = PRICE_COLOR;
//        self.time_lab4.backgroundColor = PRICE_COLOR;
//        self.time_lab3.backgroundColor = PRICE_COLOR;
//
//    }
    
    //format of time
    NSString *format_time = [NSString stringWithFormat:@"%@分钟%@秒",str_minute,str_second];
    
  //  NSLog(@"format_time : %@",format_time);
    
    return format_time;
}
@end
